package com.songjiasong.db.dao;


import java.util.*;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.songjiasong.db.DBConnection;
import com.songjiasong.db.NoteBookSchema;
import com.songjiasong.db.NoteBookVo;

public class NoteBookDao{
	
	private DBConnection helper;
	private SQLiteDatabase db;
	
	public NoteBookDao(Context ctx){
		 helper = new DBConnection(ctx);
	}
		
	public ArrayList<HashMap<String, Object>> queryNoteBookList(CharSequence date, CharSequence sString){
		db = helper.getWritableDatabase();	
		if("".equals(date)&&"".equals(sString)){
			return queryAll();
		}
		Cursor c = db.query(NoteBookSchema.TABLE_NAME, 
				new String[]{NoteBookSchema.DATE,NoteBookSchema.TITLE}, 
				NoteBookSchema.DATE+" like ? and "+ NoteBookSchema.TITLE + " like ?", 
				new String[]{"%"+date.toString()+"%","%"+sString.toString()+"%"}, null, null, NoteBookSchema.DATE);
		c.moveToFirst();
		int num = c.getCount();
		ArrayList<HashMap<String, Object>> rs =  new ArrayList<HashMap<String, Object>>();  
		for(int i = 0; i < num; i++){
			HashMap<String, Object> content = new HashMap<String, Object>();
			content.put(NoteBookSchema.DATE,c.getString(0));
			content.put(NoteBookSchema.TITLE,c.getString(1));
			rs.add(content);
			c.moveToNext();
		}
		c.close();
		db.close();
		return rs;
	}
	
	public ArrayList<HashMap<String, Object>> queryAll(){
		db = helper.getWritableDatabase();	
		Cursor c = db.query(NoteBookSchema.TABLE_NAME, 
				new String[]{NoteBookSchema.DATE,NoteBookSchema.TITLE}, 
				null, null, null, null, NoteBookSchema.DATE);
		c.moveToFirst();
		int num = c.getCount();
		ArrayList<HashMap<String, Object>> rs =  new ArrayList<HashMap<String, Object>>();  
		for(int i = 0; i < num; i++){
			HashMap<String, Object> content = new HashMap<String, Object>();
			content.put(NoteBookSchema.DATE,c.getString(0));
			content.put(NoteBookSchema.TITLE,c.getString(1));
			rs.add(content);
			c.moveToNext();
		}
		c.close();
		db.close();
		return rs;
	}
	
	public NoteBookVo findByExample(NoteBookVo vo){
		db = helper.getWritableDatabase();	
		NoteBookVo rs = new NoteBookVo();
		Cursor c = db.query(NoteBookSchema.TABLE_NAME, 
				new String[]{NoteBookSchema.ID,NoteBookSchema.DATE,NoteBookSchema.TITLE,NoteBookSchema.CONTENT}, 
				NoteBookSchema.DATE+" = ? and " + NoteBookSchema.TITLE+" = ?", 
				new String[]{vo.getDate().toString(),vo.getTitle().toString()}, null, null, null);
		c.moveToFirst();
		rs.setId(c.getInt(0));
		rs.setDate(c.getString(1));
		rs.setTitle(c.getString(2));
		rs.setContent(c.getString(3));
		db.close();
		return rs;
	}
	
	public boolean addNote(NoteBookVo vo){
		db = helper.getWritableDatabase();	
		ContentValues values = new ContentValues();
		values.put(NoteBookSchema.TITLE, vo.getTitle().toString());
		values.put(NoteBookSchema.DATE, vo.getDate().toString());
		values.put(NoteBookSchema.CONTENT, vo.getContent().toString());
		db.insert(NoteBookSchema.TABLE_NAME, null, values);
		db.close();
		return true;
	}
	
	public boolean updateNote(NoteBookVo vo){
		db = helper.getWritableDatabase();	
		ContentValues values = new ContentValues();
		values.put(NoteBookSchema.TITLE, vo.getTitle().toString());
		values.put(NoteBookSchema.DATE, vo.getDate().toString());
		values.put(NoteBookSchema.CONTENT, vo.getContent().toString());
		String where = NoteBookSchema.ID + " = " +vo.getId();
		db.update(NoteBookSchema.TABLE_NAME, values, where, null);
		db.close();
		return true;
	}
	
	public boolean deleteNote(NoteBookVo vo){
		db = helper.getWritableDatabase();	
		String where = NoteBookSchema.ID + " = " +vo.getId();
		db.delete(NoteBookSchema.TABLE_NAME, where, null);
		db.close();
		return true;
	}
}
